package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.pojo.dto.UserDTO;
import com.li.pojo.dto.UserPageDTO;
import com.li.pojo.po.PageResult;
import com.li.pojo.po.User;
import com.li.mapper.UserMapper;
import com.li.pojo.vo.UserVO;
import com.li.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-04-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setCreateBy("admin");
        user.setUpdateBy("admin");
        user.setCreateTime(LocalDateTime.now().toString());
        user.setUpdateTime(LocalDateTime.now().toString());
        userMapper.insert(user);
    }

    @Override
    public PageResult getUserList(UserPageDTO userPageDTO) {
        Page<UserVO> pageBean = PageHelper.startPage(userPageDTO.getPageNum(), userPageDTO.getPageSize());
        userMapper.getUsersList(userPageDTO);
        return new PageResult(200, "查询成功", null, pageBean.getResult(), pageBean.getTotal());
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now().toString());
        userMapper.updateUser(user);
    }

    @Override
    public List<User> getByDeptId(Integer deptId) {
        return userMapper.getByDeptId(deptId);
    }
}
